package com.sunkyc.framework.handler;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.handlers.StrictFill;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        long excuID = StpUtil.getLoginIdAsLong();
        if(ObjectUtil.isNotEmpty(excuID)){
            this.strictInsertFill(metaObject, "createBy", () -> excuID, Long.class); // 起始版本 3.3.3(推荐)
            this.strictInsertFill(metaObject, "updateBy", () -> excuID, Long.class); // 起始版本 3.3.3(推荐)
        }
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            long excuID = StpUtil.getLoginIdAsLong();
            if(ObjectUtil.isNotEmpty(excuID)){
                this.strictInsertFill(metaObject, "updateBy", () -> excuID, Long.class); // 起始版本 3.3.3(推荐)
            }
        } catch (Exception e) {
        }
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐)
    }



}
